Rail Analysis

Mapping Rail Lines and Rail Nodes in the United States.

This is a precursor project to one that will create a graph of rail, shipping, trucking, and air freight transport networks for analysis.

import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
import folium
import contextily as cx
/Users/jnapolitano/venvs/finance/lib/python3.9/site-packages/geopandas/_compat.py:111: UserWarning: The Shapely GEOS version (3.10.2-CAPI-1.16.0) is incompatible with the GEOS version PyGEOS was compiled with (3.10.1-CAPI-1.16.0). Conversions between both will be slow.
  warnings.warn(

The error above is unimportant for my use case. It is an unfortunate reality of Python Programming that package updates can break compatability layers.

Rail Line Dataset Analysis

Loading the Rail Line Data

shapefilepath = "/Users/jnapolitano/Projects/rail-mapping/North_American_Rail_Lines/North_American_Rail_Lines.shp"

line_df = gpd.read_file(shapefilepath)



line_df.head()
OBJECTID FRAARCID FRFRANODE TOFRANODE STFIPS CNTYFIPS STCNTYFIPS STATEAB COUNTRY FRADISTRCT ... CARDDIRECT NET MILES KM TIMEZONE IM_RT_TYPE DBLSTK Shape_Leng ShapeSTLen geometry
0 1 300000 348741 348746 38 015 38015 ND US 8 ... None M 0.178101 0.286626 C None None 0.003194 418.656759 LINESTRING (-100.74521 46.80285, -100.74509 46...
1 2 300001 338567 338686 30 087 30087 MT US 8 ... None O 0.886585 1.426824 M None None 0.017227 2047.012225 LINESTRING (-106.65759 45.86405, -106.65769 45...
2 3 300002 330112 330117 16 031 16031 ID US 8 ... None M 0.221820 0.356985 M None None 0.004269 483.919572 LINESTRING (-113.78532 42.54225, -113.78342 42...
3 4 300003 330113 330116 16 031 16031 ID US 8 ... None I 0.127571 0.205306 M None None 0.002484 278.254150 LINESTRING (-113.78484 42.54231, -113.78482 42...
4 5 300004 329497 329504 16 063 16063 ID US 8 ... None M 0.447515 0.720207 M DS UP-DS 0.008412 982.887938 LINESTRING (-114.40776 42.93610, -114.40763 42...

5 rows × 39 columns

Checking Cooridindate System.

The coordinate system must be in the epsg 3857 format to overlay.

line_df.crs
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

Converting to EPSG 3857 System

line_wm = line_df.to_crs(epsg=3857)

Rail Lines in US, Canada, Mexico Map

ax = line_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor='k', markersize = .5)
cx.add_basemap(ax, zoom=4)
../../_images/Rail_12_0.png

Data Fields

line_wm.columns
Index(['OBJECTID', 'FRAARCID', 'FRFRANODE', 'TOFRANODE', 'STFIPS', 'CNTYFIPS',
       'STCNTYFIPS', 'STATEAB', 'COUNTRY', 'FRADISTRCT', 'RROWNER1',
       'RROWNER2', 'RROWNER3', 'TRKRGHTS1', 'TRKRGHTS2', 'TRKRGHTS3',
       'TRKRGHTS4', 'TRKRGHTS5', 'TRKRGHTS6', 'TRKRGHTS7', 'TRKRGHTS8',
       'TRKRGHTS9', 'DIVISION', 'SUBDIV', 'BRANCH', 'YARDNAME', 'PASSNGR',
       'STRACNET', 'TRACKS', 'CARDDIRECT', 'NET', 'MILES', 'KM', 'TIMEZONE',
       'IM_RT_TYPE', 'DBLSTK', 'Shape_Leng', 'ShapeSTLen', 'geometry'],
      dtype='object')

Texas Rail Lines Data

Texas is an important freight destination. The state possess many natural important ports and freight stations.

Filtering the data set for Texas state Lines.

FIPS codes can be found at https://www.nrcs.usda.gov/wps/portal/nrcs/detail/?cid=nrcs143_013696

Texas is recognized as code 48.

#Getting Unique values to identify 48 is in the set 
print(line_wm['STFIPS'].unique())
#48 is a strin in the dataset do not compare it to an integer!!
texas_lines_df = line_wm.loc[line_wm['STFIPS'] == '48']
#print(texas_lines_df)
#print(texas_lines_df)
['38' '30' '16' '41' '02' '56' '53' '46' '27' '31' '49' '19' '06' '32'
 '04' '08' '35' '29' '17' '20' '40' '05' '18' '21' '09' '44' '25' '23'
 '34' '33' '50' '36' '42' '51' '11' '10' '39' '24' '54' '37' '22' '48'
 '28' '47' '15' '01' '13' '45' '12' '26' '55' None '99']

Texas Non-Interactive Map

ax = texas_lines_df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k', markersize = .5)
cx.add_basemap(ax, zoom=6)
../../_images/Rail_20_0.png

Texas Rail Map Interactive Map.

texas_lines_df.explore()
Make this Notebook Trusted to load map: File -> Trust Notebook